﻿
Imports System.Runtime.CompilerServices

Public Module extensions
    <Extension()> Public Function milliseconds(ByVal d As DateTime) As Int64
        Return ticksToMilliseconds(d.Ticks())
    End Function

    <Extension()> Public Function [true](ByVal s As String) As Boolean
        Return strsame(s, "1") OrElse strsame(s, "true", False)
    End Function

    <Extension()> Public Function [as](Of T)(ByVal i As Object, ByRef o As T) As Boolean
        Return convertor.cast(Of T)(i, o)
    End Function

    <Extension()> Public Function [as](Of T)(ByVal i As Object) As T
        Dim rtn As T = Nothing
        If convertor.cast(Of T)(i, rtn) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function

    <Extension()> Public Function cast(Of T)(ByVal i As fromBytes, ByRef o As T) As Boolean
        Dim bs As bytesbase(Of T) = Nothing
        Return (convertor.cast(Of bytesbase(Of T))(i, bs) AndAlso eva(o, +bs)) OrElse
               convertor.cast(Of T)(i, o)
    End Function

    <Extension()> Public Function [as](Of T)(ByVal i As fromBytes, ByRef o As T) As Boolean
        Return cast(i, o)
    End Function

    <Extension()> Public Function [as](Of T)(ByVal i As fromBytes) As T
        Dim o As T = Nothing
        If cast(i, o) Then
            Return o
        Else
            Return Nothing
        End If
    End Function

    <Extension()> Public Function asint32(ByVal i As UInt32) As Int32
        Return i
    End Function

    <Extension()> Public Function asint32(ByVal i As Int64) As Int32
        Return i
    End Function

    <Extension()> Public Function asint32(ByVal i As Double) As Int32
        Return i
    End Function

    <Extension()> Public Function asint64(ByVal i As Double) As Int64
        Return i
    End Function

    <Extension()> Public Function asint64(ByVal i As Int32) As Int64
        Return i
    End Function

    <Extension()> Public Function asint64(ByVal i As UInt32) As Int64
        Return i
    End Function

    <Extension()> Public Function asuint32(ByVal i As Int32) As UInt32
        Return i
    End Function

    <Extension()> Public Function asuint64(ByVal i As Int64) As UInt64
        Return i
    End Function

    <Extension()> Public Function has(Of T)(ByVal i() As T, ByVal j As T) As Boolean
        If i Is Nothing OrElse i.Length() = 0 Then
            Return False
        Else
            Return Array.IndexOf(i, j) <> npos
        End If
    End Function
End Module
